package edu.towson.cosc.cosc455.aloiac1.markdowncompiler;

import java.io.IOException;

public class MySemanticAnalyzer {
	/** Temporary string to hold tokens */
	private String tempString = "";

	/**
	 * Function called from main()
	 * Pops markdown out of stack, and calls convert()
	 */
	public void toHTML() throws IOException {
		while(!Compiler.completeTokens.isEmpty()) {
			tempString = Compiler.completeTokens.pop();
			convert(tempString);
		}
	}
	
	/**
	 * Changes markdown to html
	 * Uses two arrays located in the Compiler class that are organized by index.
	 */
	public void convert (String temp) { 
		if (temp.equalsIgnoreCase("#END USE")) {
			tempString = Compiler.completeTokens.pop();
			useDefineValue(tempString);
			tempString = Compiler.completeTokens.pop();
			return;
		}
		else {
			for (int count=0; count<Compiler.html.length; count++) { 
				if (temp.equalsIgnoreCase(Compiler.markdown[count]))
					temp=Compiler.html[count];
			}
		}
		Compiler.htmlStack.push(temp);
	}
	
	/**
	 * Finds corresponding DEFINE / USE name and spits out value.
	 * ELSE: spits out error
	 */
	public void useDefineValue (String temp) {
		for (int i=0; i<Compiler.define.length; i=i+2) {
			if(temp.equalsIgnoreCase(Compiler.define[i].substring(5))) {
				Compiler.htmlStack.push(Compiler.define[i+1].substring(6));
				return;
			}
		}
		System.out.println("STATIC SEMANTIC ERROR:" + temp + "does not equal any pre-determined names");
		System.exit(0);
	}
}
